package com.geostar.constructionland.index.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.geostar.constructionland.index.entity.InxAdjustConditionEntity;
import com.geostar.constructionland.index.entity.InxStorageAcotEntity;
import com.geostar.constructionland.index.entity.model.IndexAdjustModel;
import com.geostar.constructionland.index.mapper.InxAdjustConditionMapper;
import com.geostar.constructionland.index.mapper.InxStorageAcotMapper;
import com.geostar.constructionland.index.service.IndexAdjustService;
import com.geostar.constructionland.index.util.CommonUtils;
import com.geostar.constructionland.index.util.FormatUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class IndexAdjustSerImpl implements IndexAdjustService {
    @Resource
    InxStorageAcotMapper inxStorageAcotMapper;
    @Resource
    InxAdjustConditionMapper inxAdjustConditionMapper;

    @Override
    @Transactional
    public void insertOrUpdateAdjustIndex(IndexAdjustModel indexAdjustModel) {
        InxStorageAcotEntity inxStorageAcot = new InxStorageAcotEntity();
        InxAdjustConditionEntity inxAdjustCondition = new InxAdjustConditionEntity();
        String inxStorageId = indexAdjustModel.getInxStorageId();

        inxStorageAcot.setDistCode(indexAdjustModel.getDistCode());
        inxStorageAcot.setDistName(indexAdjustModel.getDistName());
        inxStorageAcot.setDistCodeCountry(indexAdjustModel.getDistCodeCountry());
        inxStorageAcot.setDistCodeCity(indexAdjustModel.getDistCodeCity());
        inxStorageAcot.setDistCodeProvince(indexAdjustModel.getDistCodeProvince());
        Date approvalTime = FormatUtils.formatStringByDate(indexAdjustModel.getApprovalTime());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY");
        inxStorageAcot.setIndexYear(Integer.parseInt(simpleDateFormat.format(approvalTime)));
        inxStorageAcot.setIndexName(indexAdjustModel.getProjectName());
        inxStorageAcot.setBuildLandAdd(indexAdjustModel.getBuildLandAdd());
        inxStorageAcot.setAgclLand(indexAdjustModel.getAgclLand());
        inxStorageAcot.setFarmland(indexAdjustModel.getFarmland());
        inxStorageAcot.setOperatorId(indexAdjustModel.getOperatorId());
        inxStorageAcot.setRemark(indexAdjustModel.getRemark());

        inxAdjustCondition.setProjectName(indexAdjustModel.getProjectName());
        inxAdjustCondition.setApprovalNum(indexAdjustModel.getApprovalNum());
        inxAdjustCondition.setApprovalTime(approvalTime);
        inxAdjustCondition.setApprovalAuthority(indexAdjustModel.getApprovalAuthority());
        inxAdjustCondition.setTotalProjectArea(indexAdjustModel.getTotalProjectArea());
        inxAdjustCondition.setBuildLandAdd(indexAdjustModel.getBuildLandAdd());
        inxAdjustCondition.setAgclLand(indexAdjustModel.getAgclLand());
        inxAdjustCondition.setFarmland(indexAdjustModel.getFarmland());
        inxAdjustCondition.setBusinessId(indexAdjustModel.getBusinessId());

        if(StringUtils.isBlank(inxStorageId)) {
            String newInxStorageId = CommonUtils.getUUID();
            inxStorageAcot.setId(newInxStorageId);
            inxStorageAcot.setIndexType("ZBTZ");
            Date nowDate = new Date();
            inxStorageAcot.setStorageTime(nowDate);
            inxStorageAcot.setIndexStatus("01");
            inxStorageAcot.setEffectiveTime(nowDate);
            inxStorageAcot.setOverallStatus("2");
            inxStorageAcot.setOverallType("0");
            inxStorageAcotMapper.insert(inxStorageAcot);

            inxAdjustCondition.setId(CommonUtils.getUUID());
            inxAdjustCondition.setInxStorageId(newInxStorageId);
            inxAdjustConditionMapper.insert(inxAdjustCondition);
        } else {
            inxStorageAcot.setId(inxStorageId);
            inxAdjustCondition.setId(indexAdjustModel.getAdjustIndexId());
            inxStorageAcotMapper.updateById(inxStorageAcot);
            inxAdjustConditionMapper.updateById(inxAdjustCondition);
        }
    }

    @Override
    @Transactional
    public void deleteAdjustIndexByIds(List<String> inxStorageIdList, List<String> adjustIndexIdList) {
        if(inxStorageIdList.size() == adjustIndexIdList.size()) {
            inxStorageAcotMapper.deleteBatchIds(inxStorageIdList);
            inxAdjustConditionMapper.deleteBatchIds(adjustIndexIdList);
        }
    }
}
